In [12]:
# Let's check our version
!cs2cs
In [13]:
# We would expect this to work, but it doesn't
# The towgs84 is used for transformation
! echo "49554 215020 0" | cs2cs -v +init=epsg:31370 +to +init=epsg:4326 -f %.6f
In [14]:
# We get a different result if we use invproj, what's going on?
! echo "49554 215020" | invproj -v +init=epsg:31370 -f %.6f
In [15]:
# We get the correct result if we fix the last parameter of the towgs84 parameter
# http://osgeo-org.1560.x6.nabble.com/proj4-epsg-31370-possible-wrong-parameters-td3841364.html
!echo "49554 215020" | cs2cs -v \
+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 \
+lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl \
+towgs84=-106.869,52.2978,-103.724,0.3366,-0.457,1.8422,-1.2747 +units=m \
+no_defs -to +proj=longlat +datum=WGS84 +ellps=WGS84 +no_defs -f %6f
In [16]:
# This does not work for invproj because proj and invproj ignore the datum shift
# The towgs84 is ignored
!echo "49554 215020" | invproj -v \
+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 \
+lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl \
+towgs84=-1,1,1,1,1,1,1 +units=m \
+no_defs \
-f %6f
In [17]:
# There is another datums shift that gives the same result on spatialreference.org
# http://spatialreference.org/ref/sr-org/epsg31370-correct-datum-shift/
!echo "49554 215020" | cs2cs -v \
+proj=lcc +lat_1=51.16666723333334 +lat_2=49.83333389999999 \
+lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 \
+ellps=intl +towgs84=-99.1,53.3,-112.5,0.419,-0.83,1.885,-1.0 +units=m +no_defs \
-to +datum=WGS84 +ellps=WGS84 -f %6f
You can also use the ogr2ogr tool, which uses the osr library.
This one is more up to date on my system. We have to define the format of our csv file in a vrt file (or use geojson).
In [18]:
%%file test.vrt
<OGRVRTDataSource>
<OGRVRTLayer name="test">
<SrcDataSource>test.csv</SrcDataSource>
<GeometryType>wkbPoint</GeometryType>
<LayerSRS>EPSG:31370</LayerSRS>
<GeometryField encoding="PointFromColumns" x="x" y="y"/>
</OGRVRTLayer>
</OGRVRTDataSource>
In [19]:
! echo "x,y\n49554,215020" >test.csv
jsontxt = ! ogr2ogr -f GeoJSON -t_srs EPSG:4326 /dev/stdout test.vrt
import json
#json.loads(jsontxt)
print("\n".join(jsontxt))
json.loads("".join(jsontxt))['features'][0]['geometry']['coordinates']
Out[19]:
In [20]:
# Or you can use a python script to talk to the osr library, which as the correct coordinates.
import osgeo.osr
src = osgeo.osr.SpatialReference()
dst = osgeo.osr.SpatialReference()
src.ImportFromEPSG(31370)
print src.GetTOWGS84()
dst.ImportFromEPSG(4326)
transform = osgeo.osr.CoordinateTransformation(src, dst)
lon, lat, z = transform.TransformPoint(49554, 215020, 0)
print(lon, lat, z)
In [20]: